package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.entity.DoctorEntity;
import com.example.demo.service.DoctorService;
import com.example.demo.tool.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@Api(tags = "医生信息相关接口")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    @PostMapping("doctor/add")
    @ApiOperation(value = "增加",notes = "时间不用增加,后台已经增加了当前时间")
    public ResponseResult add(DoctorEntity doctorEntity){
        doctorEntity.setCreateTime(new Date());
//        doctorEntity.setHireDate(new Date());
        doctorService.save(doctorEntity);
        return ResponseResult.success();
    }

    @GetMapping("listAll")
    @ApiOperation("分页")
    public ResponseResult listAll(@ApiParam("页码，默认为1") @RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,
                                  @ApiParam("每页数量，默认为10") @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize,
                                  @ApiParam("真实姓名") @RequestParam(name = "trueName",required = false) String trueName,
                                  @ApiParam("科室") @RequestParam(name = "departMent",required = false) String departMent){
        PageHelper.startPage(pageNum,pageSize);
        List<DoctorEntity> list = doctorService.selectAllDoctor(trueName,departMent);
        PageInfo<DoctorEntity>page=new PageInfo<>(list);
        return ResponseResult.success(page);
    }

    @GetMapping("doctor/getOne")
    @ApiOperation("根据id获取一条信息")
    public DoctorEntity getOne(@ApiParam("12345") @RequestParam(name = "id") Integer id){
        return doctorService.getById(id);
    }

    @GetMapping("delete/doctor")
    @ApiOperation("根据id删除一条信息")
    public ResponseResult deleteOne(Integer id){
        doctorService.removeById(id);
        return ResponseResult.success();
    }

    @PostMapping("update/doctor")
    @ApiOperation("根据id修改一条信息")
    public ResponseResult updateDoctor(Integer id,DoctorEntity doctorEntity){
        doctorService.updateById(doctorEntity);
        return ResponseResult.success();
    }

    @PostMapping("batchdelete/doctor")
    @ApiOperation("批量删除医生信息")
    public ResponseResult batchDelete(String ids){
        String[] nums = ids.split(",");
        for(int  c=0;c<nums.length;c++) {
            doctorService.removeById(nums[c]);
        }
        return ResponseResult.success();
    }
}
